using System.Collections.Generic;

namespace Battle.TimelineLog
{
    /// <summary>
    /// 战斗参与者
    /// </summary>
    public class Fighter : FightId, ITimeActive
    {
        public int Side { get; private set; }
        public HealthPoint Hp { get; } = new HealthPoint();
        public BattleProp Prop { get; } = new BattleProp();
        public FightStatus Status { get; } = new FightStatus();
        public List<Buff> Buffs { get; } = new List<Buff>();
        public List<Skill> Skills { get; } = new List<Skill>();

        public int CreateTime { get; private set; }
        public int NextTime { get; private set; }
        public int EndTime { get; private set; }
        public string SysId { get; private set; }

        public Battle Battle { get; }// 整个战斗上层数据

        public Fighter() { }
        public Fighter(Battle battle)
        {
            Battle = battle;
            CreateTime = Battle.TimeLine.CrtTime;
            EndTime = Battle.TimeLine.TotalTime;
        }

        public virtual void Active()
        {
            // 这里负责普通攻击
            CreateAction();

            // 重新设置下次攻击的时间
            NextTime = Battle.TimeLine.CrtTime + Prop.AtkSpd.Value;
        }
        public void Init(InitFighterData ifd)
        {
            Prop.Atk.SetBase(ifd.Atk);
            Prop.Def.SetBase(ifd.Def);
            Prop.AtkSpd.SetBase(ifd.AtkSpd);
            Hp.SetMaxValue(ifd.HpMax, true);
            Side = ifd.Side;
            SysId = ifd.SysId;
            ifd.Skills.ForEach(sk =>
            {
                var skRes = SysDataMgr.Sys_Skill.GetData(sk.sysId);
                if (skRes.activeType == SeEnumActiveType.ShiJian)
                {
                    var skill = new TimeSkill(sk.sysId, sk.level);
                    Skills.Add(skill);
                    Battle.InsertTimeActive(skill);
                }
                else
                {
                    var skill = new Skill(sk.sysId, sk.level);
                    Skills.Add(skill);
                }
            });
            NextTime = Battle.TimeLine.CrtTime + Prop.AtkSpd.Value;
        }

        public void CreateAction()
        {
            // 首先获取到攻击目标
            List<Fighter> targets = getTargets();
            for (int i = 0; i < targets.Count; i++)
            {
                var tar = targets[i];
                var damage = Prop.Atk.Value - tar.Prop.Def.Value;
                tar.Hp.Minus(damage);
                if (tar.IsDead())
                {
                    Battle.FighterDead(tar);
                }
            }
        }

        public bool IsDead() { return Hp.Value <= 0; }

        protected List<Fighter> getTargets()
        {
            List<Fighter> tars = new List<Fighter>();
            // 获得当前普通攻击的目标列表
            // test 当前直接获得第一个敌人 or 第一个队友
            var heroRes = getSysHero();
            if ((int)heroRes.target.camp != Side)
                tars.Add(Battle.EnemyTeam.GetAliveMember());
            else
                tars.Add(Battle.SelfTeam.GetAliveMember());
            return tars;
        }

        SysHero getSysHero()
        {
            return SysDataMgr.Sys_Hero.GetData(SysId);
        }
    }
}